package com.rika.controller;

import com.rika.pojo.ScmUser;
import com.rika.pojo.UserModel;
import com.rika.response.QueryResult;
import com.rika.response.Result;
import com.rika.service.ScmUserService;
import com.rika.service.SystemModelService;
import com.rika.service.UserModelService;
import com.rika.util.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;

@RestController
public class ScmUserController {
    @Resource
    private ScmUserService scmUserService;
    @Resource
    private SystemModelService systemModelService;
    @Resource
    private UserModelService userModelService;
    @RequestMapping("deletescumuser")
    public Result scmUserDel(String account,String modelName){
        Result result=new Result();
        Integer modelCode=systemModelService.selectByName(modelName);
        UserModel userModel=new UserModel();
        userModel.setAccount(account);
        userModel.setModelCode(modelCode);
        userModelService.deleteModel(userModel);
        if(userModelService.selectByAccount(account).size()==0){
            System.out.println(11111);
            scmUserService.deleteAccount(account);
        }
        result.setCode(200);
        result.setMsg("删除成功");
        return result;
    }
    @RequestMapping("getAll")
    public QueryResult getAll(Integer currentPage,Integer pageSize){
        QueryResult result=new QueryResult();
        Page page=new Page(currentPage,pageSize);
        Map<String,Object> params=new HashMap<>();
        params.put("first",page.getFirst());
        params.put("max",page.getMax());
        List<ScmUser> list=scmUserService.getAll(params);
        int total=scmUserService.getAllcount();
        return QueryResult.ok(total,list);
    }
    //新增
    @RequestMapping("addScmUser")
    public Result addScmUser(String account,String password,String username,String status,String modelNames){
        Result result=new Result();
        ScmUser scmUser=new ScmUser();
        scmUser.setAccount(account);
        scmUser.setPassword(password);
        scmUser.setName(username);
        scmUser.setStatus(Integer.parseInt(status));
        System.out.println(scmUser);
        scmUserService.addScmUser(scmUser);
        String[] list=modelNames.split(",");
        for(int i=0;i<list.length;i++){
            Integer modelCode=systemModelService.selectByName(list[i]);
            UserModel userModel=new UserModel();
            userModel.setAccount(account);
            userModel.setModelCode(modelCode);
            userModelService.addUserModel(userModel);
        }
        result.setCode(200);
        result.setMsg("新增成功");
        return result;
    }

    //修改回填
    @RequestMapping("selectByAccount")
    public Result selectByAccount(String account){
        Result result=new Result();
        ScmUser scmUser=scmUserService.selectByAccount(account);
        result.setCode(200);
        result.setMsg("ok");
        result.setData(scmUser);
        return result;
    }
    //修改回填权限
    @RequestMapping("selectModelName")
    public Result selectAllModelName(String account){
        Result result=new Result();
        List<Integer> modelCode=userModelService.selectByAccount(account);
        List<String> list=new ArrayList<>();
        for(int i=0;i<modelCode.size();i++){
            String s=systemModelService.selectByModelCode(modelCode.get(i));
            list.add(s);
        }
        result.setCode(200);
        result.setData(list);
        return result;
    }
    @RequestMapping("updateScmUser")
    public Result updateScmUser(ScmUser scmUser,String modelNames){
        Result result=new Result();
        scmUserService.updateScmUser(scmUser);
        userModelService.deleteAllByAccount(scmUser.getAccount());
        String[] list=modelNames.split(",");
        for(int i=0;i<list.length;i++){
            Integer modelCode=systemModelService.selectByName(list[i]);
            UserModel userModel=new UserModel();
            userModel.setAccount(scmUser.getAccount());
            userModel.setModelCode(modelCode);
            userModelService.addUserModel(userModel);
        }
        result.setCode(200);
        result.setMsg("修改成功");
        return result;
    }
    @RequestMapping("selectAccountByName")
    public Result selectAccountByName(String name){
        Result result=new Result();
        List<ScmUser> list=scmUserService.selectAccountByName(name);
        result.setCode(200);
        result.setData(list);
        return result;
    }
}
